package InterFaceIModel;

import java.util.ArrayList;
import java.util.List;

import DataBase.DatabaseImplement;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class LoaiThuChiModel implements ILoaiThuChi {

	private long id;
	private String tenloai;
	private String ghichu;
	private int isthuchi;
	DatabaseImplement databaseImplement;
	static DatabaseImplement data;
	@Override
	public long getId() {
		// TODO Auto-generated method stub
		return id;
	}

	@Override
	public String getTenLoai() {
		// TODO Auto-generated method stub
		return tenloai;
	}

	@Override
	public String getGhichu() {
		// TODO Auto-generated method stub
		return ghichu;
	}

	@Override
	public int getIsThuchi() {
		// TODO Auto-generated method stub
		return isthuchi;
	}

	@Override
	public void setId(long id) {
		// TODO Auto-generated method stub
		this.id=id;
	}

	@Override
	public void setTenLoai(String ten) {
		// TODO Auto-generated method stub
		tenloai=ten;
	}

	@Override
	public void setGhichu(String ghichu) {
		// TODO Auto-generated method stub
		this.ghichu=ghichu;
	}

	@Override
	public void setIsThucThi(int isthucthi) {
		// TODO Auto-generated method stub
		isthuchi=isthucthi;
	}
//method chinh
	@Override
	public List<ILoaiThuChi> getAll(int isthuchi,Context context) {
		// TODO Auto-generated method stub
		if (databaseImplement==null) {
			
			databaseImplement=new DatabaseImplement(context);
		}
		Cursor cursor=databaseImplement.GetAll("loaihang", null, "isthuchi=?", new String[]{String.valueOf(isthuchi)}, null, null, null);
		
		cursor.moveToFirst();
		List<ILoaiThuChi>list=new ArrayList<ILoaiThuChi>();
		
		while (!cursor.isAfterLast()) {
			
			long Id=cursor.getInt(cursor.getColumnIndex("id"));
			String tenLoaiString=cursor.getString(cursor.getColumnIndex("tenloai"));
			String ghichu=cursor.getString(cursor.getColumnIndex("ghichu"));
			ILoaiThuChi loaiThuChi=new LoaiThuChiModel();
			loaiThuChi.setGhichu(ghichu);
			loaiThuChi.setId(Id);
			loaiThuChi.setTenLoai(tenLoaiString);
			list.add(loaiThuChi);
			
			cursor.moveToNext();
			
		}
		return list;
	}
    public static ILoaiThuChi getById(long id,Context context)
    {
    	if (data==null) {
			
			data=new DatabaseImplement(context);
		}
		Cursor cursor=data.GetAll("loaihang", null, "id=?", new String[]{String.valueOf(id)}, null, null, null);
		
		cursor.moveToFirst();
		ILoaiThuChi list=new LoaiThuChiModel();
		
		while (!cursor.isAfterLast()) {
			
			long Id=cursor.getInt(cursor.getColumnIndex("id"));
			String tenLoaiString=cursor.getString(cursor.getColumnIndex("tenloai"));
			String ghichu=cursor.getString(cursor.getColumnIndex("ghichu"));
			
			list.setGhichu(ghichu);
			list.setId(Id);
			list.setTenLoai(tenLoaiString);
			
			
			cursor.moveToNext();
			
		}
		return list;
    }
	@Override
	public long insert(ILoaiThuChi iLoaiThuChi, Context context) {
		// TODO Auto-generated method stub
		if (databaseImplement==null) {
			
			databaseImplement=new DatabaseImplement(context);
		}
		ContentValues contentValues=new ContentValues();
		
		contentValues.put("tenloai", iLoaiThuChi.getTenLoai());
		contentValues.put("ghichu", iLoaiThuChi.getGhichu());
		contentValues.put("isthuchi", iLoaiThuChi.getIsThuchi());
		return databaseImplement.InsertCommand(contentValues, "loaihang", null);
	}

	@Override
	public int update(ILoaiThuChi iLoaiThuChi, Context context) {
		// TODO Auto-generated method stub
		if 		(databaseImplement==null) {
			
			databaseImplement=new DatabaseImplement(context);
		}
		ContentValues contentValues=new ContentValues();
		
		contentValues.put("tenloai", iLoaiThuChi.getTenLoai());
		contentValues.put("ghichu", iLoaiThuChi.getGhichu());
		contentValues.put("isthuchi", iLoaiThuChi.getIsThuchi());
		
	return databaseImplement.UpdateCommand("loaihang", "id=?", new String[]{String.valueOf(iLoaiThuChi.getId())}, contentValues);
	}

	@Override
	public int delete(long id,Context context) {
		// TODO Auto-generated method stub
if (databaseImplement==null) {
			
			databaseImplement=new DatabaseImplement(context);
		}
		return databaseImplement.DeleteCommand("loaihang", "id=?", new String[]{String.valueOf(id)});
	}

}
